package com.ouc.pampas;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Maps;
import java.util.Locale;
import java.util.Map;
public enum ThreadVars
{
  APP,  DOMAIN,  PORT,  LOCALE;
  
  @SuppressWarnings("unchecked")
private static ThreadLocal<Map<ThreadVars, Object>> currentEnv = new ThreadLocal()
  {
    protected Map<ThreadVars, Object> initialValue()
    {
      return Maps.newHashMap();
    }
  };
  
  private ThreadVars() {}
  
  private static <T> T get(ThreadVars var)
  {
    return (T) (currentEnv.get()).get(var);
  }
  
  private static void set(ThreadVars var, Object value)
  {
    ((Map)currentEnv.get()).put(var, value);
  }
  
  private static void clear(ThreadVars var)
  {
    ((Map)currentEnv.get()).remove(var);
  }
  
  public static void clearAll()
  {
    ((Map)currentEnv.get()).clear();
  }
  
  public static void setApp(App app)
  {
    set(APP, app);
  }
  
  public static App getApp()
  {
    return (App)get(APP);
  }
  
  public static void clearApp()
  {
    clear(APP);
  }
  
  public static String getAppKey()
  {
    return getApp().getKey();
  }
  
  public static void setDomain(String domain)
  {
    set(DOMAIN, domain);
  }
  
  public static String getDomain()
  {
    return (String)MoreObjects.firstNonNull(get(DOMAIN), getApp().getDomain());
  }
  
  public static void clearDomain()
  {
    clear(DOMAIN);
  }
  
  public static void setPort(Integer port)
  {
    set(PORT, port);
  }
  
  public static void clearPort()
  {
    clear(PORT);
  }
  
  public static String getHost()
  {
    Integer port = (Integer)MoreObjects.firstNonNull(get(PORT), Integer.valueOf(80));
    if (port.intValue() == 80) {
      return getDomain();
    }
    return getDomain() + ":" + port;
  }
  
  public static void setLocale(String locale)
  {
    set(LOCALE, locale);
  }
  
  public static String getLocale()
  {
    return (String)MoreObjects.firstNonNull(get(LOCALE), Locale.getDefault().toString());
  }
  
  public static void clearLocale()
  {
    clear(LOCALE);
  }
}
